import axios from "axios";
import qs from 'qs'
import store from "../store";


const server = axios.create({
  // axios中请求配置有baseURL选项，表示请求URL公共部分
  baseURL: process.env.VUE_APP_BASE_API,
  // baseURL: "http://localhost:8080/blog",
  // 超时
  timeout: 10000,
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  },
});

// request拦截器
server.interceptors.request.use(
  config => {
    config.data = qs.stringify(config.data);
    return config;
  },
  error => {
    console.log('err' + error);
    return Promise.reject(error);
  }
);

// 响应拦截器
server.interceptors.response.use(response => {
    // return response.data;
    const code = response.data.code
    if (code !== 200) {
      return Promise.reject('error')
    } else {
      return response.data
    }
  },
  error => {
    console.log('err' + error)
    return Promise.reject(error)
  }
);

export default server;
